﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;


namespace Cdn.Blogger.Controllers
{
    public class FileStatus
    {
        public string Url { get; set; }
        public string MD5 { get; set; }
        public int Size { get; set; }
        public string Name { get; set; }
        public string Callback { get; set; }
        public string Option { get; set; }
    }
    public class AttachmentController : UIController
    { 
        public ActionResult Index()
        {
            return View();
        }

        public string Upload(string id)
        {
            /*!
            FileDrop Revamped - server-side upload handler sample
            in public domain  | http://filedropjs.org

            ***

            This is an example of server-side script that handles both AJAX and IFrame uploads.

            AJAX upload provides raw file data as POST input while IFrame is a POST request
            with Request.Files member set.

            Result is either output as HTML with JavaScript code to invoke the callback
            (like JSONP) or in plain text if none is given (it's usually absent on AJAX).
            */

            // Callback name is passed if upload happens via iframe, not AJAX (FileAPI).
            FileStatus upload = new FileStatus();
            string callback = Request.Form["fd-callback"];
            upload.Callback = callback;
            string name;
            byte[] data;

            // Upload data can be POST'ed as raw form data or uploaded via <iframe> and <form>
            // using regular multipart/form-data enctype (which is handled by ASP.NET Request.Files).
            var fdFile = Request.Files["fd-file"];
            if (fdFile != null)
            {
                // Regular multipart/form-data upload.
                name = fdFile.FileName;
                data = new byte[fdFile.ContentLength];
                fdFile.InputStream.Read(data, 0, fdFile.ContentLength);

                upload.Name = name;
                upload.Size = fdFile.ContentLength;

            }
            else
            {
                // Raw POST data.
                name = HttpUtility.UrlDecode(Request.Headers["X-File-Name"]);
                data = new byte[Request.InputStream.Length];
                Request.InputStream.Read(data, 0, (int)Request.InputStream.Length); //up to 2GB

                upload.Name = name;
                upload.Size = (int)Request.InputStream.Length;
            }

            // Output message for this demo upload. In your real app this would be something
            // meaningful for the calling script (that uses FileDrop.js).
            byte[] md5Hash;
            using (MD5 md5 = MD5.Create())
            {
                md5Hash = md5.ComputeHash(data);
              
            }
            string output = string.Format("{0}; received {1} bytes, MD5 = {2}", name, data.Length, BytesArrayToHexString(md5Hash));
            upload.MD5 = BytesArrayToHexString(md5Hash);
            string path = Server.MapPath("~/Content/Uploaded");
            System.IO.File.WriteAllBytes(path + "\\" + name, data);

            // In FileDrop sample this demonstrates the passing of custom ?query variables along
            // with an AJAX/iframe upload.
            string opt = Request["upload_option"];
            upload.Option = opt;

            if (!string.IsNullOrEmpty(opt))
            {
                output += "\nReceived upload_option with value " + opt;
            }

            if (!string.IsNullOrEmpty(callback))
            {
                output = HttpUtility.JavaScriptStringEncode(output);
                output +=   "<!DOCTYPE html><html><head></head><body><script type=\"text/javascript\">" +
                   "try{window.top." + callback + "(\"" + output + "\")}catch(e){}</script></body></html>";
                return output;
            }
            else
            {
                output = Newtonsoft.Json.JsonConvert.SerializeObject(upload);
                return output;
            }
        }
        public string BytesArrayToHexString(byte[] hash)
        {
            var sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }
    }
}
